﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace Transportr.Pages
{
    public partial class LineDetails : PhoneApplicationPage
    {
        List<int> current_lines;
        LineDetailsViewModel ViewModel = null;

        public LineDetails()
        {
            InitializeComponent();
            this.ViewModel = new LineDetailsViewModel();
            this.DataContext = this.ViewModel;
            this.current_lines = new List<int>();
            this.Loaded += new RoutedEventHandler(LineDetails_Loaded);
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                if (this.NavigationContext.QueryString.ContainsKey("lnId"))
                {
                    this.current_lines.Add(Int32.Parse(this.NavigationContext.QueryString["lnId"]));
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Invalid line info query!");
            }
            base.OnNavigatedTo(e);
        }

        void LineDetails_Loaded(object sender, RoutedEventArgs e)
        {
            if (!this.ViewModel.LineDetails.IsDataLoaded)
            {
                this.ViewModel.LineDetails.BeginUpdate(new Transportr.Metadata.LineDetailFilter() { LineIds = current_lines });
            }
        }

        private void appbarSwitchDirection_Click(object sender, EventArgs e)
        {
            if (this.ViewModel.CurrentItem != null)
            {
                this.ViewModel.CurrentItem.ReverseRoute = !this.ViewModel.CurrentItem.ReverseRoute;
            }
        }

        private void appbarFavorites_Click(object sender, EventArgs e)
        {
            if (this.ViewModel.CurrentItem != null)
            {
                if (this.ViewModel.CurrentItem.IsFavorite)
                    this.ViewModel.CurrentItem.RemoveFromFavorites();
                else
                    this.ViewModel.CurrentItem.AddToFavorites();

                App.ViewModel.FavoriteLines.BeginUpdate();
            }
        }

    }
}